// page/component/changePassword/index.js
const {request} = require('../../../utils/request');

Page({
  data: {
    newPassword: '',
    confirmPassword: ''
  },

  onoldPasswordInput(e) {
    this.setData({
      oldPassword: e.detail.value
    });
  },

  onNewPasswordInput(e) {
    this.setData({
      newPassword: e.detail.value
    });
  },

  onConfirmPasswordInput(e) {
    this.setData({
      confirmPassword: e.detail.value
    });
  },

  async submitChangePassword() {
    const {oldPassword ,newPassword, confirmPassword} = this.data;
    if (newPassword !== confirmPassword) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      });
      return;
    }

    request({
      url: '/user/change_password',
      method: 'POST',
      data: {
        new_password: newPassword,
        old_password: oldPassword
      },
      showLoading: true
    })
    .then((res) => {
      if (res.code === 2000) {
        wx.showToast({
          title: '修改密码成功',
          icon: 'success'
        });
        wx.navigateBack();
      } else {
        wx.showToast({
          title: '修改密码失败',
          icon: 'none'
        });
        console.error('修改密码失败:', res.message);
      }
    })
    .catch((error) => {
      wx.showToast({
        title: '请求出错',
        icon: 'none'
      });
      console.error('修改密码请求出错:', error);
    });
  }
})
